home *** CD-ROM | disk | FTP | other *** search
/ Visual Basic Source Code / Visual Basic Source Code.iso / vbsource / databa_1 / tablelst.frm (.txt) < prev    next >
Encoding:
Visual Basic Form  |  1999-07-10  |  18.0 KB  |  514 lines

  1. VERSION 5.00
  2. Object = "{831FDD16-0C5C-11D2-A9FC-0000F8754DA1}#2.0#0"; "MSCOMCTL.OCX"
  3. Begin VB.Form frmTablesTVW 
  4.    BorderStyle     =   3  'Fixed Dialog
  5.    Caption         =   "List of Tables"
  6.    ClientHeight    =   4380
  7.    ClientLeft      =   2760
  8.    ClientTop       =   3750
  9.    ClientWidth     =   7410
  10.    Icon            =   "TablelsTVW.frx":0000
  11.    LinkTopic       =   "Form1"
  12.    LockControls    =   -1  'True
  13.    MaxButton       =   0   'False
  14.    MDIChild        =   -1  'True
  15.    MinButton       =   0   'False
  16.    ScaleHeight     =   4380
  17.    ScaleWidth      =   7410
  18.    ShowInTaskbar   =   0   'False
  19.    Begin VB.CommandButton cmdFldProperty 
  20.       Caption         =   "Fld property"
  21.       Height          =   375
  22.       Left            =   4890
  23.       TabIndex        =   11
  24.       Top             =   3780
  25.       Width           =   1035
  26.    End
  27.    Begin VB.CommandButton cmdTable 
  28.       Caption         =   "Table"
  29.       Height          =   375
  30.       Left            =   3720
  31.       TabIndex        =   10
  32.       Top             =   3780
  33.       Width           =   1035
  34.    End
  35.    Begin VB.CommandButton cmdClose 
  36.       Caption         =   "Close"
  37.       Height          =   375
  38.       Left            =   6060
  39.       TabIndex        =   0
  40.       Top             =   3780
  41.       Width           =   1035
  42.    End
  43.    Begin MSComctlLib.TreeView tvwDB 
  44.       Height          =   3210
  45.       Left            =   180
  46.       TabIndex        =   3
  47.       Top             =   900
  48.       Width           =   3210
  49.       _ExtentX        =   5662
  50.       _ExtentY        =   5662
  51.       _Version        =   393217
  52.       Style           =   7
  53.       ImageList       =   "ImageList1"
  54.       Appearance      =   1
  55.    End
  56.    Begin MSComctlLib.ListView lvwProperties 
  57.       Height          =   2130
  58.       Left            =   3690
  59.       TabIndex        =   9
  60.       Top             =   1530
  61.       Width           =   3435
  62.       _ExtentX        =   6059
  63.       _ExtentY        =   3757
  64.       View            =   3
  65.       LabelEdit       =   1
  66.       LabelWrap       =   -1  'True
  67.       HideSelection   =   0   'False
  68.       FullRowSelect   =   -1  'True
  69.       _Version        =   393217
  70.       Icons           =   "ImageList1"
  71.       SmallIcons      =   "ImageList1"
  72.       ForeColor       =   -2147483640
  73.       BackColor       =   -2147483643
  74.       BorderStyle     =   1
  75.       Appearance      =   1
  76.       BeginProperty Font {0BE35203-8F91-11CE-9DE3-00AA004BB851} 
  77.          Name            =   "MS Sans Serif"
  78.          Size            =   8.25
  79.          Charset         =   0
  80.          Weight          =   400
  81.          Underline       =   0   'False
  82.          Italic          =   0   'False
  83.          Strikethrough   =   0   'False
  84.       EndProperty
  85.       NumItems        =   0
  86.    End
  87.    Begin MSComctlLib.ImageList ImageList1 
  88.       Left            =   2850
  89.       Top             =   330
  90.       _ExtentX        =   1005
  91.       _ExtentY        =   1005
  92.       BackColor       =   -2147483643
  93.       ImageWidth      =   16
  94.       ImageHeight     =   16
  95.       MaskColor       =   12632256
  96.       _Version        =   393216
  97.       BeginProperty Images {2C247F25-8591-11D1-B16A-00C0F0283628} 
  98.          NumListImages   =   5
  99.          BeginProperty ListImage1 {2C247F27-8591-11D1-B16A-00C0F0283628} 
  100.             Picture         =   "TablelsTVW.frx":030A
  101.             Key             =   "imgDatabase"
  102.          EndProperty
  103.          BeginProperty ListImage2 {2C247F27-8591-11D1-B16A-00C0F0283628} 
  104.             Picture         =   "TablelsTVW.frx":0556
  105.             Key             =   "imgClosed"
  106.          EndProperty
  107.          BeginProperty ListImage3 {2C247F27-8591-11D1-B16A-00C0F0283628} 
  108.             Picture         =   "TablelsTVW.frx":0652
  109.             Key             =   "imgOpen"
  110.          EndProperty
  111.          BeginProperty ListImage4 {2C247F27-8591-11D1-B16A-00C0F0283628} 
  112.             Picture         =   "TablelsTVW.frx":0766
  113.             Key             =   "imgField"
  114.          EndProperty
  115.          BeginProperty ListImage5 {2C247F27-8591-11D1-B16A-00C0F0283628} 
  116.             Picture         =   "TablelsTVW.frx":0878
  117.             Key             =   "imgProp"
  118.          EndProperty
  119.       EndProperty
  120.    End
  121.    Begin VB.Label lblFieldName 
  122.       BorderStyle     =   1  'Fixed Single
  123.       Height          =   285
  124.       Left            =   4905
  125.       TabIndex        =   8
  126.       Top             =   1170
  127.       Width           =   2130
  128.    End
  129.    Begin VB.Label lblField 
  130.       Caption         =   "Field:"
  131.       Height          =   195
  132.       Left            =   4320
  133.       TabIndex        =   7
  134.       Top             =   1215
  135.       Width           =   510
  136.    End
  137.    Begin VB.Label lblTable 
  138.       Caption         =   "Table:"
  139.       Height          =   195
  140.       Left            =   4320
  141.       TabIndex        =   6
  142.       Top             =   900
  143.       Width           =   510
  144.    End
  145.    Begin VB.Label lblTableName 
  146.       BorderStyle     =   1  'Fixed Single
  147.       Height          =   285
  148.       Left            =   4905
  149.       TabIndex        =   5
  150.       Top             =   855
  151.       Width           =   2130
  152.    End
  153.    Begin VB.Label lblFieldProperties 
  154.       Caption         =   "Field properties:"
  155.       Height          =   195
  156.       Left            =   3780
  157.       TabIndex        =   4
  158.       Top             =   585
  159.       Width           =   1140
  160.    End
  161.    Begin VB.Label lblTableList 
  162.       Caption         =   "Tables && Fields:"
  163.       Height          =   195
  164.       Left            =   180
  165.       TabIndex        =   2
  166.       Top             =   585
  167.       Width           =   2310
  168.    End
  169.    Begin VB.Label lblDatabase 
  170.       Caption         =   "Database:"
  171.       Height          =   420
  172.       Left            =   180
  173.       TabIndex        =   1
  174.       Top             =   45
  175.       Width           =   6315
  176.    End
  177. Attribute VB_Name = "frmTablesTVW"
  178. Attribute VB_GlobalNameSpace = False
  179. Attribute VB_Creatable = False
  180. Attribute VB_PredeclaredId = True
  181. Attribute VB_Exposed = False
  182. ' TablesTVW.frm
  183. ' By Herman Liu
  184. Option Explicit
  185. Dim rsSchema As adodb.Recordset
  186. Dim rs As adodb.Recordset
  187. Dim mNodeDB As node
  188. Dim mNodeTables As node
  189. Dim mnodeFields As node
  190. Dim mListItem As ListItem
  191. Dim mCurrListRef           ' Flag to assure this node is already clicked
  192. Private Sub Form_Load()
  193.     On Error Resume Next
  194.     lblDatabase = "Database: " + gFileSpec
  195.      ' Configure ListView control.
  196.     lvwProperties.ListItems.Clear
  197.     lvwProperties.Icons = ImageList1
  198.     lvwProperties.SmallIcons = ImageList1
  199.     lvwProperties.ColumnHeaders.Clear
  200.     lvwProperties.ColumnHeaders.Add , , "Property", 1700
  201.     lvwProperties.ColumnHeaders.Add , , "Value", lvwProperties.Width - 1700
  202.     lvwProperties.View = lvwReport
  203.         ' we want to show coloumn headers
  204.     lvwProperties.HideColumnHeaders = False
  205.         ' We don't want shade/highlight any item at start
  206.     lvwProperties.HideSelection = True
  207.         
  208.        'Configure TreeView
  209.     tvwDB.Nodes.Clear
  210.     Set gAcnn = New adodb.Connection
  211.     gAcnn.CursorLocation = adUseClient
  212.     gAcnn.Open "PROVIDER=Microsoft.Jet.OLEDB.3.51;Data Source=" & gFileSpec & ";"
  213.        ' If you use 4.0
  214.     ' gAcnn.Open "PROVIDER=Microsoft.Jet.OLEDB.4.0;Data Source=" & gFileSpec & ";"
  215.     Set rsSchema = gAcnn.OpenSchema(adSchemaTables, Array(Empty, Empty, Empty, "TABLE"))
  216.     Me.MousePointer = vbHourglass
  217.     '--------------------------------------------------------------------
  218.     ' Set root node of TreeView.
  219.     '--------------------------------------------------------------------
  220.     Set mNodeDB = tvwDB.Nodes.Add(, , "RootDB", "Database", "imgDatabase")
  221.     mNodeDB.Tag = "RootDB"
  222.      
  223.     '------------------------
  224.     ' Set table nodes.
  225.     '------------------------
  226.     Dim mtblName As String
  227.     Do Until rsSchema.EOF
  228.          ' Since MS current schema returns tables named "MSys...."
  229.          ' as well as their TABLE_TYPE is also "TABLE", we exclude them.
  230.         If UCase(Left(rsSchema!Table_name, 4)) <> "MSYS" Then
  231.            If UCase(Left(rsSchema!Table_name, 11)) <> "SWITCHBOARD" Then
  232.                mtblName = rsSchema!Table_name
  233.                Set mNodeTables = tvwDB.Nodes.Add("RootDB", tvwChild, "X" & mtblName, mtblName, "imgClosed")
  234.                
  235.                mNodeTables.Tag = "Tables"
  236.            End If
  237.         End If
  238.         rsSchema.MoveNext
  239.     Loop
  240.     rsSchema.Close
  241.     If mNodeTables Is Nothing Then
  242.         Me.MousePointer = vbDefault
  243.         MsgBox "No table in the database"
  244.         Exit Sub
  245.     End If
  246.     ' REDIM gstrFields and gstrFieldsOrig according to No. of tables in DB
  247.     Dim mNumTables As Integer
  248.     mNumTables = tvwDB.Nodes("RootDB").Children
  249.     ReDim gstrFields(mNumTables, 2) As String
  250.     ReDim gstrFieldsOrig(mNumTables, 2) As String
  251.     '----------------------------------------------------------------------
  252.     ' Set field nodes for each table (at the same time fill first dimension
  253.     '   of gstrFields and gstrFieldsOrig)
  254.     ' We only want field Name, Type, DefinedSize, Attributes & NumericScale
  255.     ' totalling 6.
  256.     '----------------------------------------------------------------------
  257.     Dim i As Integer, n As Integer, j As Integer
  258.     Dim mctn As Integer
  259.     Dim mType As Long
  260.     Dim mAttr As Integer
  261.     Dim mstrType As String
  262.     Dim mstrAttr As String
  263.     Dim mFldName As String
  264.     Set rs = New adodb.Recordset
  265.     mctn = tvwDB.Nodes("RootDB").Children
  266.       ' We begin with the first table, but what is its node index in Nodex?
  267.     n = tvwDB.Nodes("RootDB").Child.FirstSibling.Index
  268.       ' The Nodes collection is a 1-based collection.
  269.     For i = n To mctn + n
  270.        mtblName = tvwDB.Nodes(i).Text
  271.        
  272.          ' We have the table name, so we Query that table in order to get
  273.          ' the field names.  1 record is more than good enough, hence the
  274.          ' second argument is 1.
  275.            Set rs = gAcnn.Execute("SELECT * FROM [" & mtblName & "]", 1, 1)
  276.        
  277.        For j = 0 To rs.Fields.Count - 1
  278.             mFldName = rs.Fields(j).Name
  279.             Set mnodeFields = tvwDB.Nodes.Add("X" & mtblName, tvwChild, "X" & mFldName, mFldName, "imgField")
  280.             mnodeFields.Tag = "Fields"
  281.        Next
  282.        rs.Close
  283.        
  284.        ' Fill first dimension of gstrFields and gstrFieldsOrig with table name
  285.        ' (starting with subscript 0)
  286.        gstrFields(i - n, 1) = mtblName
  287.        gstrFieldsOrig(i - n, 1) = mtblName
  288.     Next i
  289.      ' Sort the top group of nodes.
  290.     tvwDB.Nodes(1).Sorted = True
  291.      ' Expand top node.
  292.     tvwDB.Nodes(1).Expanded = True
  293.     Me.MousePointer = vbDefault
  294.     Exit Sub
  295. End Sub
  296. ' Called from tvwDB_Dblclick
  297. Private Sub GetProperties(mtblName, mFldName, mNodeKey)
  298.     ' Query that retrieves the fields
  299.     Set rs = gAcnn.Execute("SELECT * FROM [" & mtblName & "]", 1, 1)
  300.     lvwProperties.ListItems.Clear
  301.     Dim i As Integer
  302.     Dim mthisFldName As String
  303.     Dim mType As Long
  304.     Dim mAttr
  305.     Dim mstrType As String
  306.     Dim mstrAttr As String
  307.     Dim mDefinedSize
  308.     Dim mNumericScale
  309.     Dim mPrecision
  310.     For i = 0 To rs.Fields.Count - 1
  311.         If CStr(rs.Fields(i).Name) = mFldName Then
  312.              mthisFldName = rs.Fields(i).Name
  313.                 mType = rs.Fields(i).Type
  314.              mstrType = ConvType(mType)
  315.                 mAttr = rs.Fields(i).Attributes
  316.              mstrAttr = ConvAttr(mAttr)
  317.              mDefinedSize = rs.Fields(i).DefinedSize
  318.              mNumericScale = rs.Fields(i).NumericScale
  319.              mPrecision = rs.Fields(i).Precision
  320.              
  321.              Set mListItem = lvwProperties.ListItems.Add(, , _
  322.                   Text:="Name:", SmallIcon:="imgProp")
  323.              mListItem.SubItems(1) = mthisFldName
  324.              
  325.              Set mListItem = lvwProperties.ListItems.Add(, , _
  326.                   Text:="Type:", SmallIcon:="imgProp")
  327.              mListItem.SubItems(1) = mstrType
  328.              
  329.              Set mListItem = lvwProperties.ListItems.Add(, , _
  330.                   Text:="Attributes:", SmallIcon:="imgProp")
  331.              mListItem.SubItems(1) = mstrAttr
  332.              
  333.              Set mListItem = lvwProperties.ListItems.Add(, , _
  334.                   Text:="DefinedSize:", SmallIcon:="imgProp")
  335.              mListItem.SubItems(1) = mDefinedSize
  336.              
  337.              Set mListItem = lvwProperties.ListItems.Add(, , _
  338.                   Text:="NumericScale:", SmallIcon:="imgProp")
  339.              mListItem.SubItems(1) = mNumericScale
  340.              
  341.              Set mListItem = lvwProperties.ListItems.Add(, , _
  342.                   Text:="Precision:", SmallIcon:="imgProp")
  343.              mListItem.SubItems(1) = mPrecision
  344.              Exit For
  345.         End If
  346.     Next i
  347.     lblTableName.Caption = Space(4) & mtblName
  348.     lblFieldName.Caption = Space(4) & mFldName
  349.     rs.Close
  350.     Set rs = Nothing
  351.     mCurrListRef = mNodeKey
  352. End Sub
  353. Private Sub tvwDB_Expand(ByVal node As node)
  354.     If node.Tag = "Tables" Then
  355.         node.Image = "imgOpen"
  356.     End If
  357. End Sub
  358. Private Sub tvwDB_Collapse(ByVal node As node)
  359.     If node.Tag = "Tables" Then
  360.         node.Image = "imgClosed"
  361.     End If
  362. End Sub
  363. Private Sub tvwDB_DblClick()
  364.    Dim tmpNode As node
  365.    Dim tmpText As String
  366.    Dim tmpKey
  367.    Dim tmpTag
  368.    Dim tmpParentText As String
  369.    Dim tmplistitem As ListItem
  370.        ' Set the variable to the SelectedItem
  371.    Set tmpNode = tvwDB.SelectedItem
  372.        ' Retrieve properties of the node
  373.    tmpText = tmpNode.Text
  374.    tmpKey = tmpNode.Key
  375.    tmpTag = tmpNode.Tag
  376.    If tmpTag = "RootDB" Then
  377.         Exit Sub
  378.    End If
  379.    If tmpTag = "Tables" Then
  380.         ' Invoke the frmDBGrid form, then EXIT
  381.         If ExistFormCap(tmpText) = False Then
  382.               ' Supply table name as frmGrid's caption
  383.               ' Each copy object of frmDBGrid has to use
  384.               ' a different name.  We use that of table name.
  385.               ' Create a variant, assign table name as its
  386.               ' name before assigning frmDBGrid to it.
  387.             Dim tmpObject
  388.             tmpObject = tmpText
  389.             Set tmpObject = New frmDBGrid
  390.                ' frmDBGrid needs the value of gTableName
  391.             gTableName = tmpText
  392.                
  393.             tmpObject.Show
  394.                ' Form caption being the table name (so that later
  395.                ' we are able to check if the form caption bearing
  396.                ' the table name already exists)
  397.             tmpObject.Caption = tmpText
  398.         Else
  399.             MsgBox "Table is already opened"
  400.         End If
  401.         Exit Sub
  402.    End If
  403.    If tmpTag = "Fields" Then
  404.         tmpParentText = tmpNode.Parent.Text
  405.         If mCurrListRef <> tmpKey Then
  406.              GetProperties tmpParentText, tmpText, tmpKey
  407.         End If
  408.         
  409.         tmpNode.Sorted = True
  410.         
  411.          ' Enusre field name property is readily visible
  412.         Set tmplistitem = lvwProperties.FindItem(tmpText, , , lvwPartial)
  413.         If Not (tmplistitem Is Nothing) Then
  414.              tmplistitem.EnsureVisible
  415.         End If
  416.    End If
  417. End Sub
  418. Private Sub cmdTable_Click()
  419.    If tvwDB.SelectedItem Is Nothing Then
  420.        MsgBox "Please select a table first"
  421.        Exit Sub
  422.    End If
  423.        
  424.    Dim tmpNode As node
  425.    Dim tmpText As String
  426.    Dim tmpKey
  427.    Dim tmpTag
  428.    Dim tmpParentText As String
  429.    Dim tmplistitem As ListItem
  430.        ' Set the variable to the SelectedItem
  431.    Set tmpNode = tvwDB.SelectedItem
  432.        ' Retrieve properties of the node
  433.    tmpText = tmpNode.Text
  434.    tmpKey = tmpNode.Key
  435.    tmpTag = tmpNode.Tag
  436.    If tmpTag = "RootDB" Or tmpTag = "Fields" Then
  437.        MsgBox "Please select a table first"
  438.        Exit Sub
  439.    End If
  440.    If tmpTag = "Tables" Then
  441.         ' Invoke the frmDBGrid form, then EXIT
  442.         If ExistFormCap(tmpText) = False Then
  443.                ' Supply table name as frmGrid's caption
  444.             Dim tmpObject
  445.             tmpObject = tmpText
  446.             Set tmpObject = New frmDBGrid
  447.                ' frmDBGrid needs the value of gTableName
  448.             gTableName = tmpText
  449.                
  450.             tmpObject.Show
  451.                ' Form caption being the table name (so that later
  452.                ' we are able to check if the form caption bearing
  453.                ' the table name already exists)
  454.             tmpObject.Caption = tmpText
  455.         Else
  456.             MsgBox "Table is already opened"
  457.         End If
  458.         Exit Sub
  459.    End If
  460. End Sub
  461. Private Sub cmdFldProperty_Click()
  462.    If tvwDB.SelectedItem Is Nothing Then
  463.        MsgBox "Please select a field of a table first"
  464.        Exit Sub
  465.    End If
  466.    Dim tmpNode As node
  467.    Dim tmpText As String
  468.    Dim tmpKey
  469.    Dim tmpTag
  470.    Dim tmpParentText As String
  471.    Dim tmplistitem As ListItem
  472.        ' Set the variable to the SelectedItem
  473.    Set tmpNode = tvwDB.SelectedItem
  474.        ' Retrieve properties of the node
  475.    tmpText = tmpNode.Text
  476.    tmpKey = tmpNode.Key
  477.    tmpTag = tmpNode.Tag
  478.    If tmpTag = "RootDB" Or tmpTag = "Tables" Then
  479.        MsgBox "Please select a field of a table first"
  480.        Exit Sub
  481.    End If
  482.    If tmpTag = "Fields" Then
  483.         tmpParentText = tmpNode.Parent.Text
  484.         If mCurrListRef <> tmpKey Then
  485.              GetProperties tmpParentText, tmpText, tmpKey
  486.         End If
  487.         
  488.         tmpNode.Sorted = True
  489.         
  490.          ' Enusre field name property is readily visible
  491.         Set tmplistitem = lvwProperties.FindItem(tmpText, , , lvwPartial)
  492.         If Not (tmplistitem Is Nothing) Then
  493.              tmplistitem.EnsureVisible
  494.         End If
  495.    End If
  496. End Sub
  497. Function ExistFormCap(FormCap As String) As Boolean
  498.     Dim mExistFormCap As Boolean
  499.     mExistFormCap = False
  500.     Dim i As Integer
  501.     For i = Forms.Count - 1 To 0 Step -1
  502.         If UCase(Forms(i).Caption) = UCase(FormCap) Then
  503.              mExistFormCap = True
  504.              Exit For
  505.         End If
  506.     Next
  507.     ExistFormCap = mExistFormCap
  508. End Function
  509. Private Sub cmdClose_Click()
  510.    On Error Resume Next
  511.    gAcnn.Close
  512.    Unload Me
  513. End Sub
  514.